//
//  PatrolServiceClient.m
//  Patrol
//
//  Created by 范武 on 13-2-22.
//  Copyright (c) 2013年 FSTI. All rights reserved.
//

#import "PatrolServiceClient.h"
#import "AFNetworking.h"
#import "FWJspJsonRequestOperation.h"

#define kServerAddress @"http://222.76.124.9"
#define kLoginPath @"/intf-gis/login.jsp"
#define kSearchPath @"/intf-gis/worklist/query.jsp"
#define kSearchAllPath @"/intf-gis/worklist/all.jsp"
#define kSearchPlainPath @"/intf-gis/worklist/content.jsp"
#define kWorkDetailPath @"/intf-gis/workitem/itemdetail.jsp"
#define kSearchWithGps @"/intf-gis/worklist/querygps.jsp"
#define kExecuteWorkOrder @"/intf-gis/workitem/itemdeal.jsp"
#define kUploadPicPath @"/intf-gis/workitem/itemupload.jsp"

@implementation PatrolServiceClient

+ (PatrolServiceClient *)shareInstance
{
    static PatrolServiceClient * _shareClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shareClient = [[PatrolServiceClient alloc] initWithBaseURL:[NSURL URLWithString:kServerAddress]];
    });
    return _shareClient;
}

- (id)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
    if (self) {
        [self registerHTTPOperationClass:[FWJspJsonRequestOperation class]];
        self.parameterEncoding = AFJSONParameterEncoding;
    }
    return self;
}


- (void)loginWithUsername:(NSString *)username password:(NSString *)password resultBlock:(outJsonBlock)block
{

    NSDictionary * param = @{@"username":username, @"password" : password};
    [self postPath:kLoginPath parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (block) {
            block(responseObject,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil, error);
        }
    }];
}

- (void)searchWithTerm:(NSString *)username searchTerm:(NSString *)searchTerm resultBlock:(outJsonBlock)block{
    NSDictionary * param = @{@"workNo" : username, @"query" : searchTerm};
    [self postPath:kSearchTermPath parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (block) {
            block(responseObject,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil, error);
        }
    }];
}

- (void)searchAll:(NSString *)username pageSize:(NSInteger)pageSize pageNumber:(NSInteger)pageNumber resultBlock:(outJsonBlock)block{
    NSDictionary * param = @{@"workNo":username, @"page_size" : [NSString stringWithFormat:@"%d",pageSize],@"page_number":[NSString stringWithFormat:@"%d",pageNumber]};
    [self postPath:kSearchAllPath parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (block) {
            block(responseObject,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil, error);
        }
    }];
}

- (void)searchPlain:(NSString *)username workID:(NSString *)workID resultBlock:(outJsonBlock)block{
    NSDictionary * param = @{@"workNo":username, @"list_id" : workID};
    [self postPath:kSearchPlainPath parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (block) {
            block(responseObject,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil, error);
        }
    }];
}

- (void)getUser:(NSString *)username Work:(NSString *)workId resultBlock:(outJsonBlock)block
{
    NSDictionary * param = @{@"workNo":username, @"work_id":workId};
    [self postPath:kWorkDetailPath parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (block) {
            block(responseObject, nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil, error);
        }
    }];
}

- (void)searchWithGps:(NSString *)username latitude:(double)latitude longitude:(double)longitude resultBlock:(outJsonBlock)block
{
    NSDictionary * param = @{@"workNo":username, @"latitude" : [NSString stringWithFormat:@"%f",latitude],@"longitude" : [NSString stringWithFormat:@"%f",longitude]};
    [self postPath:kSearchWithGps parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (block) {
            block(responseObject,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil, error);
        }
    }];
}

- (void)submitWork:(NSString *)workID task:(NSInteger)taskID location:(CLLocationCoordinate2D)geoLocation formValues:(NSArray *)info resultBlock:(outJsonBlock)block
{
    NSDictionary * param1 = @{@"workNo":workID, @"task_id":[NSNumber numberWithInteger:taskID], @"latitude":[NSNumber numberWithDouble:geoLocation.latitude], @"longitude":[NSNumber numberWithDouble:geoLocation.longitude]};
    
    NSDictionary * form = @{@"form_values": info};
    
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithDictionary:param1];
    [param addEntriesFromDictionary:form];
    DLog(@"发送的报文：%@",param);
    [self postPath:kExecuteWorkOrder parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (block) {
            block(responseObject,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil, error);
        }
    }];
    
}

- (void)cancelHttpRequest:(NSString *)path{
    [self cancelAllHTTPOperationsWithMethod:@"POST" path:path];
}

- (void)uploadPic:(UIImage *)img name:(NSString *)picName taskID:(NSInteger)taskID
{
    NSData * imgData = UIImageJPEGRepresentation(img, 0);
    NSMutableURLRequest * request = [self multipartFormRequestWithMethod:@"POST" path:kUploadPicPath parameters:@{@"task_id":[NSNumber numberWithInteger:taskID]} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imgData name:@"uploadfile" fileName:picName mimeType:@"image/jpeg"];
    }];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    [self enqueueHTTPRequestOperation:operation];
}
@end
